package com.xueyi.aixihu.config;

import com.alibaba.fastjson.JSON;
import com.xueyi.aixihu.entity.generatedMapper.DtAuthGroupMapper;
import com.xueyi.aixihu.entity.generatedMapper.DtAuthRuleMapper;
import com.xueyi.aixihu.entity.po.DtAuthGroup;
import com.xueyi.aixihu.entity.po.DtAuthGroupExample;
import com.xueyi.aixihu.entity.po.DtAuthRule;
import com.xueyi.aixihu.entity.po.DtAuthRuleExample;
import com.xueyi.aixihu.entity.vo.DtAuthGroupVo;
import com.xueyi.aixihu.interceptors.RequestInterceptor;
import com.xueyi.aixihu.utils.FinalUtil;
import com.xueyi.aixihu.utils.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 描述 配置工厂
 *
 * @auther 俞烨
 * @date 2022-08-13 14
 */
@SpringBootConfiguration
public class AxhConfigurationFactory implements WebMvcConfigurer {


    @Autowired
    private DtAuthGroupMapper dtAuthGroupMapper;
    @Autowired
    private DtAuthRuleMapper dtAuthRuleMapper;

    @PostConstruct
    public void initRule() {

        // 查询出所有角色的信息
        DtAuthGroupExample groupExample = new DtAuthGroupExample();
        groupExample.createCriteria().andStatusEqualTo(Boolean.TRUE);
        List<DtAuthGroup> dtAuthGroups = dtAuthGroupMapper.selectByExampleWithBLOBs(groupExample);

        Jedis jedis = JedisUtil.getJedis();
        try {
            jedis.del("ROLE_ALL_CDZ");
            for (DtAuthGroup dtAuthGroup : dtAuthGroups) {
                DtAuthGroupVo dtAuthGroupVo = new DtAuthGroupVo(dtAuthGroup);
                // 存入Redis
                jedis.hset("ROLE_ALL_CDZ", "ROLE:ID_CDZ" + dtAuthGroup.getId(), JSON.toJSONString(dtAuthGroupVo));
            }
        } finally {
            FinalUtil.unLockAndCloseRedis(jedis);
        }
    }


    @PostConstruct
    public void initRequestPathAuth() {


        DtAuthRuleExample dtAuthRuleExample = new DtAuthRuleExample();
        dtAuthRuleExample.createCriteria().andStatusEqualTo(Boolean.TRUE);
        List<DtAuthRule> dtAuthRules = dtAuthRuleMapper.selectByExample(dtAuthRuleExample);
        Jedis jedis = JedisUtil.getJedis();
        try {
            jedis.del("ALL_RULE");
            Pipeline pipelined = jedis.pipelined();
            for (DtAuthRule dtAuthGroup : dtAuthRules) {
                // 存入Redis
                pipelined.hset("ALL_RULE", dtAuthGroup.getName(), dtAuthGroup.getId().toString());
            }
            pipelined.sync();
        } finally {
            FinalUtil.unLockAndCloseRedis(jedis);
        }
    }

    @Bean
    public HandlerInterceptor requestInterceptor() {
        return new RequestInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(requestInterceptor()).addPathPatterns("/axh/**").excludePathPatterns("/axh/dtUsers/loginDtUsers");
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}
